package com.yuexunit.finance.project.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.PickupApplicationWarehouseEntity;
import com.yuexunit.finance.project.entity.lexicon.WarePickupStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;


/**
*@version 创建时间：2017年8月1日 下午2:29:56
*@author wenwenlong
*类说明
*/

@Implements(PickupApplicationWarehouseService.class)
public class PickupApplicationWarehouseServiceImpl implements PickupApplicationWarehouseService {

	@Override
	public Long createPickupApplicationWarehouse(PickupApplicationWarehouseEntity entity) {
		entity.setOutplanFlag("1");
		
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		entity.setEnabled(Boolean.TRUE);
		entity.setPickupNumber(SettingConstants.PRE_NUMBER_WAREHOUSE_PICKUP + RandomUtil.generateTimeNumber());
		entity.setAuditState(Boolean.TRUE);
		entity.setAuditRemark("");
		entity.setSubmissionStatus(Boolean.FALSE);
		entity.setAuditDate(null);
		entity.setAuditorId(null);
		entity.setAuditor("");
		
		entity.setAuditState2(Boolean.TRUE);
		entity.setAuditRemark2("");
		entity.setSubmissionStatus2(Boolean.FALSE);
		entity.setAuditDate2(null);
		entity.setAuditorId2(null);
		entity.setAuditor2("");

		entity.setAuditState3(Boolean.TRUE);
		entity.setAuditRemark3("");
		entity.setSubmissionStatus3(Boolean.FALSE);
		entity.setAuditDate3(null);
		entity.setAuditorId3(null);
		entity.setAuditor3("");

		entity.setAuditState4(Boolean.TRUE);
		entity.setAuditRemark4("");
		entity.setSubmissionStatus4(Boolean.FALSE);
		entity.setAuditDate4(null);
		entity.setAuditorId4(null);
		entity.setAuditor4("");

		entity.setAuditState5(Boolean.TRUE);
		entity.setAuditRemark5("");
		entity.setSubmissionStatus5(Boolean.FALSE);
		entity.setAuditDate5(null);
		entity.setAuditorId5(null);
		entity.setAuditor5("");

		entity.setAuditState6(Boolean.TRUE);
		entity.setAuditRemark6("");
		entity.setSubmissionStatus6(Boolean.FALSE);
		entity.setAuditDate6(null);
		entity.setAuditorId6(null);
		entity.setAuditor6("");

		entity.setAuditState7(Boolean.TRUE);
		entity.setAuditRemark7("");
		entity.setSubmissionStatus7(Boolean.FALSE);
		entity.setAuditDate7(null);
		entity.setAuditorId7(null);
		entity.setAuditor7("");

		entity.setAuditState8(Boolean.TRUE);
		entity.setAuditRemark8("");
		entity.setSubmissionStatus8(Boolean.FALSE);
		entity.setAuditDate8(null);
		entity.setAuditorId8(null);
		entity.setAuditor8("");

		entity.setAuditState9(Boolean.TRUE);
		entity.setAuditRemark9("");
		entity.setSubmissionStatus9(Boolean.FALSE);
		entity.setAuditDate9(null);
		entity.setAuditorId9(null);
		entity.setAuditor9("");
		PersistUtil.insertOrUpdate(entity);
		return entity.getPickupId();
	}

	@Override
	@Transactional
	public void updatePickupApplicationWarehouse(PickupApplicationWarehouseEntity entity) {
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(entity);
	}

	@Override
	public PickupApplicationWarehouseEntity inquirePickupApplicationWarehouseDetail(Long pickupId) {
		return PersistUtil.get(PickupApplicationWarehouseEntity.class, pickupId);
	}
	
	@Override
	public PickupApplicationWarehouseEntity inquirePickupApplicationWarehouseDetailByPickupNumber(String pickupNumber) {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationWarehouseEntity.class);
		criteria.add(Restrictions.eq("pickupNumber", pickupNumber));
		List<PickupApplicationWarehouseEntity> pawes = PersistUtil.get(criteria);
		if (pawes != null && pawes.size() > 0) {
			return pawes.get(0);
		}
		return null;
	}

	@Override
	public void disabledPickupApplicationWarehouse(Long pickupId) {
		PickupApplicationWarehouseEntity entity = PersistUtil.get(PickupApplicationWarehouseEntity.class, pickupId);
		entity.setUpdateDateTime(DateUtil.now());
		entity.setEnabled(Boolean.FALSE);
		PersistUtil.update(entity);
	}
	
	@Override
	public List<PickupApplicationWarehouseEntity> inquirePickupApplicationWarehouseList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationWarehouseEntity.class);
		return PersistUtil.get(criteria);
	}
	
	@Override
	public PageList<PickupApplicationWarehouseEntity> inquirePickupApplicationWarehousePageList(
			List<WarePickupStateEnum> pickupState,  List<Long> pickupIds, Long enterpriseId, String pickupNumber, String creator,
			Date screatDt, Date ecreatDt, String buyer, String buyer1, Date spickupDt, Date epickupDt, Long creatorId, PageCriteria page) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationWarehouseEntity.class);
		if (pickupIds != null && pickupIds.size() > 0) {
			criteria.add(Restrictions.in("pickupId", pickupIds));
		}
		if (pickupState != null && pickupState.size() > 0) {
			if (pickupState.get(0) == WarePickupStateEnum.WAIT_PLATFORM_SIGN) {
				criteria.add(Restrictions.or(Restrictions.eq("pickupState", WarePickupStateEnum.WAIT_PLATFORM_SIGN), Restrictions.eq("pickupState1", WarePickupStateEnum.WAIT_PLATFORM_SIGN)));
			} else {
				criteria.add(Restrictions.in("pickupState", pickupState));
			}
		}
		if (StringUtil.isNotBlank(pickupNumber)) {
			criteria.add(Restrictions.like("pickupNumber", "%" + pickupNumber + "%"));
		}
		if (StringUtil.isNotBlank(creator)) {
			criteria.add(Restrictions.like("creator", "%" + creator + "%"));
		}
		if (StringUtil.isNotBlank(buyer)) {
			criteria.add(Restrictions.like("buyer", "%" + buyer + "%"));
		}
		if (StringUtil.isNotBlank(buyer1)) {
			criteria.add(Restrictions.or(Restrictions.eq("buyer", buyer1), Restrictions.eq("enterpriseId", enterpriseId)));
		} else {
			if (enterpriseId != null) {
				criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
			}
		}
		if (screatDt != null) {
			criteria.add(Restrictions.ge("createDateTime", screatDt));
		}
		if (ecreatDt != null) {
			criteria.add(Restrictions.le("createDateTime", ecreatDt));
		}
		if (spickupDt != null) {
			criteria.add(Restrictions.ge("pickupDate", spickupDt));
		}
		if (epickupDt != null) {
			criteria.add(Restrictions.le("pickupDate", epickupDt));
		}
		criteria.addOrder(Order.desc("updateDateTime"));
		
//		PageList<PickupApplicationWarehouseEntity> list = PersistUtil.get(criteria, page);
		
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<Long> countPickupApplicationWarehouse(List<WarePickupStateEnum> pickupState) {
		DetachedCriteria criteria = DetachedCriteria.forClass(PickupApplicationWarehouseEntity.class);
		if (pickupState != null && pickupState.size() > 0 && pickupState.get(0) == WarePickupStateEnum.WAIT_PLATFORM_SIGN) {
			criteria.add(Restrictions.or(Restrictions.in("pickupState", pickupState), Restrictions.eq("pickupState1", WarePickupStateEnum.WAIT_PLATFORM_SIGN)));
		} else {
			criteria.add(Restrictions.in("pickupState", pickupState));
		}
		criteria.setProjection(Projections.groupProperty("pickupId"));
		return PersistUtil.get(criteria);
	}
}
